jQuery(function(){
	var serverData = {};
	serverData.key ='';
    var	serverTable;
    jQuery(document).ready(function () {
    	serverTable=  jQuery('#allTicketTable').DataTable({
    		"sScrollX": 2000,
			"sScrollXInner": 2000,
			"bScrollCollapse": true,
    		          "serverSide": true,
    		          "ordering": false,
			            "ajax": {
			                "dataType": "json",
			                "type": "get",
			                "cache": false,
			                "url":window.rootPath+ '/ui/afterSaleOutHouse/getpage',
			                "data": function (d) {
			                    return $.extend({}, d, serverData);
			                }
			            },
			            "iDisplayLength": 5,
			            columns: [
			                {
			                	data: null,
			                	defaultContent: ""
			                },
			                {
			                    data: null,
			                    defaultContent: "",
			                    render: function (data, type, full, meta) {
			                        var operate =' <input type="checkbox" class="checkchild" />';
			                        return operate;

			                    }
			                },
			                {
			                    data: 'afterSaleTicket.manageCode'
			                },
			                {
			                    data: 'createTime',
			                    render: function (data, type, full, meta) {
		                            if (data)
		                                return moment(data).format('YYYY/MM/DD');
		                            else
		                                return '';
		                       }
			                },
			                {
			                    data: 'sendReason',
				                render:function(data,type,full,meta){
				                	if(data == null || data == undefined || data == ""){
										return data;
									}else{
										return (data.length > 5) ? '<font title="'+data+'">'+data.substring(0,5)+'...'+'</font>' : data;
									}
								}
			                },
			                {
			                    data: 'afterSaleTicket.unitName',
				                render:function(data,type,full,meta){
				                	if(data == null || data == undefined || data == ""){
										return data;
									}else{
										return (data.length > 5) ? '<font title="'+data+'">'+data.substring(0,5)+'...'+'</font>' : data;
									}
								}
			                },
			                {
			                    data: 'afterSaleTicket.contractCode',
				                render:function(data,type,full,meta){
				                	if(data == null || data == undefined || data == ""){
										return data;
									}else{
										return (data.length > 5) ? '<font title="'+data+'">'+data.substring(0,5)+'...'+'</font>' : data;
									}
								}
			                },
			                {
			                	data:'afterSaleTicket.afterSaleType'
			                },
			                {
			                	data:'afterSaleTicket.proposer'
			                },
			                {
			                	data:'afterSaleTicket.superior'
			                },
			                {
			                	data:'afterSaleTicket.linkMan'
			                },
			                {
			                	data:'afterSaleTicket.linkManPhone'
			                },
			             
			                {
			                	data:'afterSaleTicket.deviceCode'
			                },
			                {
			                	data:'afterSaleTicket.deviceName',
				                render:function(data,type,full,meta){
				                	if(data == null || data == undefined || data == ""){
										return data;
									}else{
										return (data.length > 5) ? '<font title="'+data+'">'+data.substring(0,5)+'...'+'</font>' : data;
									}
								}
			                },
			                {
			                	data:'afterSaleTicket.deviceNumber'
			                },
			                {
			                	data: 'afterSaleTicket.questionDetail',
				                render:function(data,type,full,meta){
				                	if(data == null || data == undefined || data == ""){
										return data;
									}else{
										return (data.length > 5) ? '<font title="'+data+'">'+data.substring(0,5)+'...'+'</font>' : data;
									}
								}
			                },
			                {
			                	data: 'afterSaleTicket.questionTrance',
				                render:function(data,type,full,meta){
				                	if(data == null || data == undefined || data == ""){
										return data;
									}else{
										return (data.length > 5) ? '<font title="'+data+'">'+data.substring(0,5)+'...'+'</font>' : data;
									}
								}
			                }
			                ],
			            "drawCallback": function (oSettings) {
			                var k = 0;
			                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;
			                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
			                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
			                    k++;
			                }
			            }
			        });

			    });
    
    //我的服务单列表
  //获取选中状态
	$(function(){
		  $(":radio").click(function(){
			  serverData1.key = jQuery('#keyword1').val();
			  var outState=$('input:radio[name="isReceived"]:checked').val();
			  serverData1.outState = outState;
			  serverTable1.draw();
		  });
	});
    //已接收信息列表
	//  获取显示页面数据服务器分页
	var outState =  $('input:radio[name="isReceived"]:checked').val();
	var serverData1 = {};
	serverData1.outState = outState;
	serverData1.key = jQuery('#keyword1').val();
	//serverData1.key ='';
    var	serverTable1;
    jQuery(document).ready(function () {
    	serverTable1=  jQuery('#myTicketTable').DataTable({
    		"sScrollX": 2000,
			"sScrollXInner": 2000,
			"bScrollCollapse": true,
    		          "serverSide": true,
    		          "ordering": false,
			            "ajax": {
			                "dataType": "json",
			                "type": "get",
			                "cache": false,
			                "url":window.rootPath+ '/ui/afterSaleOutHouse/getMyTicketPage',
			                "data": function (d) {
			                    return $.extend({}, d, serverData1);
			                }
			            },
			            "iDisplayLength": 5,
			            columns: [
			                {
			                	data: null,
			                	defaultContent: ""
			                },
			                {
			                    data: null,
			                    defaultContent: "",
			                    render: function (data, type, full, meta) {
			                        var operate =' <input type="checkbox" class="checkchildTicket" />';
			                        return operate;

			                    }
			                },
			                {
			                	data: null,
			                	 render: function (data, type, full, meta) {
				                        var dOut;
				                        if(data.outState=="0"){
				                            dOut = '<td class="center" ><span class="badge label-pill label-danger">未接收</span></td>';
				                        }     
				                        else if(data.outState=="1") {
				                            dOut = '<td class="center" ><span class="badge label-pill label-primary">已接收</span></td>';
				                        }   
				                        else if(data.outState=="2") {
				                            dOut = '<td class="center" ><span class="badge label-pill label-primary">跟踪中</span></td>';
				                        } 
				                        else if(data.outState=="3") {
				                            dOut = '<td class="center" ><span class="badge label-pill label-success">待回访</span></td>';
				                        } 
				                        else if(data.outState=="4") {
				                            dOut = '<td class="center" ><span class="badge label-pill label-success">完成</span></td>';
				                        } 
				                        return dOut;
				                    }
			                },
			                {
			                    data: 'afterSaleTicket.manageCode'
			                },
			                {
			                    data: 'createTime',
			                    render: function (data, type, full, meta) {
		                            if (data)
		                                return moment(data).format('YYYY/MM/DD');
		                            else
		                                return '';
		                       }
			                },
			                {
			                    data: 'sendReason',
				                render:function(data,type,full,meta){
				                	if(data == null || data == undefined || data == ""){
										return data;
									}else{
										return (data.length > 5) ? '<font title="'+data+'">'+data.substring(0,5)+'...'+'</font>' : data;
									}
								}
			                },
			                {
			                    data: 'afterSaleTicket.unitName',
				                render:function(data,type,full,meta){
				                	if(data == null || data == undefined || data == ""){
										return data;
									}else{
										return (data.length > 5) ? '<font title="'+data+'">'+data.substring(0,5)+'...'+'</font>' : data;
									}
								}
			                },
			                {
			                    data: 'afterSaleTicket.contractCode',
				                render:function(data,type,full,meta){
				                	if(data == null || data == undefined || data == ""){
										return data;
									}else{
										return (data.length > 4) ? '<font title="'+data+'">'+data.substring(0,4)+'...'+'</font>' : data;
									}
								}
			                },
			                {
			                	data:'afterSaleTicket.afterSaleType'
			                },
			                {
			                    data: 'attachNumber',
			                    render: function (data, type, full, meta) {
			                    	var dOut;
			                    	if(data==null){
			                    		dOut='<td class="center" ><span class="badge label-pill label-primary">'+0+'</span></td>'
			                    	}else{
			                    		dOut='<td class="center" ><span class="badge label-pill label-primary">'+data+'</span></td>'
			                    	}
			                    	return dOut;
			                    }
			                    
			                },
			                {
			                	data:'afterSaleTicket.proposer'
			                },
			                {
			                	data:'afterSaleTicket.superior'
			                },
			                {
			                	data:'afterSaleTicket.linkMan'
			                },
			                {
			                	data:'afterSaleTicket.linkManPhone'
			                },
			                {
			                	data:'afterSaleTicket.deviceCode'
			                },
			                {
			                	data:'afterSaleTicket.deviceName',
				                render:function(data,type,full,meta){
				                	if(data == null || data == undefined || data == ""){
										return data;
									}else{
										return (data.length > 4) ? '<font title="'+data+'">'+data.substring(0,4)+'...'+'</font>' : data;
									}
								}
			                },
			                {
			                	data:'afterSaleTicket.deviceNumber',
				                render:function(data,type,full,meta){
				                	if(data == null || data == undefined || data == ""){
										return data;
									}else{
										return (data.length > 4) ? '<font title="'+data+'">'+data.substring(0,4)+'...'+'</font>' : data;
									}
								}
			                },
			                {
			                	data: 'afterSaleTicket.questionDetail',
				                render:function(data,type,full,meta){
				                	if(data == null || data == undefined || data == ""){
										return data;
									}else{
										return (data.length > 6) ? '<font title="'+data+'">'+data.substring(0,6)+'...'+'</font>' : data;
									}
								}
			                },
			                {
			                	data: 'afterSaleTicket.questionTrance',
				                render:function(data,type,full,meta){
				                	if(data == null || data == undefined || data == ""){
										return data;
									}else{
										return (data.length > 7) ? '<font title="'+data+'">'+data.substring(0,7)+'...'+'</font>' : data;
									}
								}
			                	
			                }
			                ],
			            "drawCallback": function (oSettings) {
			                var k = 0;
			                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;
			                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
			                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
			                    k++;
			                }
			            }
			        });

			    });
    //接收服务单
    jQuery('.receive').on('click', function() {
		var selects = getSelect("#allTicketTable", 1);
		 if (selects.length == 0) {
			message('请选择要接收的服务单');
		 }
		 else if(selects.length>1){
			 message('一次只能接收的服务单'); 
		 }
		 else if(selects.length ==1){
			 var id=selects[0].id;
				var ticketId=selects[0].afterSaleTicket.id;
				var unitName=selects[0].afterSaleTicket.unitName;
				var d={
						"isReceived":1,
						"outState":1
				}
				var url = window.rootPath + '/api/afterSaleOutHouses/'+id;
				jQuery.ajax({
					url : encodeURI(url),
					type : 'patch',
					data : JSON.stringify(d),
					dataType : 'json',
					contentType : 'application/json;charset=utf-8',
					success : function() {
						var url1 = window.rootPath + '/api/afterSaleTickets/'+ticketId;
				    	var d1={
				    			"isSubmit":4
						}
						jQuery.ajax({
							url : encodeURI(url1),
							type : 'patch',
							data : JSON.stringify(d1),
							dataType : 'json',
							contentType : 'application/json;charset=utf-8',
							success : function() {
								var operateType = "售后服务执行";
								var content = "接收售后服务单【"+unitName+"】";
								addOperateLog(operateType,content);
								delTask(id,"待办派遣单")//删除待办任务
							}
						});									
						serverTable.draw(false);
						serverTable1.draw(false);
					}
				}); 
			 
			}
    });
    //跟踪服务单
    jQuery('.tranceBtn').on('click', function() {
    	var selects = getSelect("#myTicketTable", 1);
		if (selects.length > 1) {
			message('一次只能跟踪一条派遣单');
		} else if (selects.length == 0) {
			message('请选择要跟踪的派遣单');
		} else if (selects.length == 1) {
			var id = selects[0].afterSaleTicket.id;//派遣单对应的服务单ID
			var outServiceId=selects[0].id;
			var outState = selects[0].outState;
			var type=selects[0].afterSaleTicket.afterSaleType;
			if (outState>=3) {
				message('派遣单已经待访或完成，不能跟踪！');
			}else{
				url=window.rootPath+"/ui/afterSaleOutHouse/trance"+"?ticketId="+id+"&type="+type+"&outServiceId="+outServiceId;
				window.location=url;
			}		
		}
    });
	//查看详情
	jQuery(".detailBtn").on('click',function(){
		var selects = getSelect("#myTicketTable", 1);
		if (selects.length > 1) {
			$.springLemonDialog({
				content : '一次只能查看一条记录',// 确认文本
				clickButton : function(sender, modal, index) {// 按钮事件处理
					$(this).closeDialog(modal);
				}
			});
		} else if (selects.length == 0) {
			$.springLemonDialog({
				content : '请选择要查看的记录',// 确认文本
				clickButton : function(sender, modal, index) {// 按钮事件处理
					$(this).closeDialog(modal);
				}
			});
		} else if (selects.length == 1) {
			if(selects[0].isSubmit==0){
				$.springLemonDialog({
					content : '未提交的记录没有详情',// 确认文本
					clickButton : function(sender, modal, index) {// 按钮事件处理
						$(this).closeDialog(modal);
					}
				});
			}
			else{
			var id = selects[0].afterSaleTicket.id;
			var afterSaleType = selects[0].afterSaleTicket.afterSaleType;
				url=window.rootPath+"/ui/afterSaleTicket/ticketDetail"+"?ticketId="+id+"&afterSaleType="+afterSaleType;
				window.location=url;
			}
		}
	});
    //完成派遣单
    jQuery('.doneTicket').on('click', function() {
    	var selects = getSelect("#myTicketTable", 1);
		if (selects.length > 1) {
			$.springLemonDialog({
				content : '一次只能完结一条派遣单',// 确认文本
				clickButton : function(sender, modal, index) {// 按钮事件处理
					$(this).closeDialog(modal);
				}
			});
		} else if (selects.length == 0) {
			$.springLemonDialog({
				content : '请选择要完结的派遣单',// 确认文本
				clickButton : function(sender, modal, index) {// 按钮事件处理
					$(this).closeDialog(modal);
				}
			});
		} else if (selects.length == 1) {
			var ticketId = selects[0].afterSaleTicket.id;
			var outHouseId = selects[0].id;
			var creatorId=selects[0].afterSaleTicket.user.id;
			var outState = selects[0].outState;
			if (outState==3) {
				message('派遣单已经完成！')
			}else{
				$("#ticketId").val(ticketId);
				$("#creatorId").val(creatorId);
				$("#ticketName").val(selects[0].afterSaleTicket.unitName);
				$("#outHouseId").val(outHouseId);
				$("#manageCode").val(selects[0].afterSaleTicket.manageCode);
				$("#contractCode").val(selects[0].afterSaleTicket.contractCode);
				$("#deviceName").val(selects[0].afterSaleTicket.deviceName);
				$("#deviceNumber").val(selects[0].afterSaleTicket.deviceNumber);
				$("#afterSummary").val("");
				$("#quoteANum").html('<td class="center" ><span class="badge label-pill label-primary">'+selects[0].attachNumber+'</span></td>');
				$("#doneTicketModal").modal('show');
			}
			
		}
    });
    //完成派遣单的保存
    jQuery('.doneBtn').on('click', function() {
    	var outHouseId= $("#outHouseId").val();
    	var ticketId = $("#ticketId").val();
    	var creatorId=$("#creatorId").val();
    	var url = window.rootPath + '/api/afterSaleOutHouses/'+outHouseId;
    	var d={
				"isFinish":1,
				"afterSummary":$("#afterSummary").val(),
				"outState":3
		}
		jQuery.ajax({
			url : encodeURI(url),
			type : 'patch',
			data : JSON.stringify(d),
			dataType : 'json',
			contentType : 'application/json;charset=utf-8',
			success : function() {
				var url1 = window.rootPath + '/ui/afterSaleOutHouse/getTicketState?ticketId='+ticketId;
				jQuery.ajax({
					url : encodeURI(url1),
					cache:false,
					type : 'get',
					dataType : 'json',
					success : function(data) {
						if(data.code==0){
							var d1={
									"isSubmit":5
							}
							var url = window.rootPath + '/api/afterSaleTickets/'+ticketId;
							jQuery.ajax({
								url : encodeURI(url),
								type : 'patch',
								data : JSON.stringify(d1),
								
								dataType : 'json',
								contentType : 'application/json;charset=utf-8',
								success : function() {
									
									addTask("售后服务单待回访","/ui/afterSaleAssess",creatorId,0,ticketId)/// 插入待办任务
								}
							});
						}
						serverTable1.draw(false);
						var operateType = "派遣服务执行";
						var content = "完结派遣单【"+jQuery("#ticketName").val()+"】";
						addOperateLog(operateType,content);
						
						$("#doneTicketModal").modal('hide');
					}

				});
			}
		});
    	
		
    });
    //查询
    jQuery('.searchBtn').on('click', function() {
		serverData.key = jQuery('#keyword').val();
		serverTable.draw();
	});
	// 撤销查询
	jQuery('.cancelBtn').on("click", function() {
		jQuery('#keyword').val('');
		serverData.key = '';
		serverTable.draw();
	});
	///////////我的派遣单查询
	/*jQuery('#ticketState').change(function(){
		serverData1.key = jQuery('#keyword1').val();
		var outState=jQuery('#ticketState').val();
		serverData1.outState=outState;
		serverTable1.draw();
	});*/
    //查询1
    jQuery('.searchBtn1').on('click', function() {
		serverData1.key = jQuery('#keyword1').val();
		//var outState=jQuery('#ticketState').val();
		var outState =  $('input:radio[name="isReceived"]:checked').val();
		serverData1.outState=outState;
		serverTable1.draw();
	});
	// 撤销查询1
	jQuery('.cancelBtn1').on("click", function() {
		jQuery('#keyword1').val('');
		//jQuery('#ticketState').val('');
		serverData1.key = '';
		var outState =  $('input:radio[name="isReceived"]:checked').val();
		serverData1.outState=outState;
		serverTable1.draw();
	});
	   ///////列表中的全选
    $("#checkAll").click(function(){
    	var check = $(this).prop("checked");
        $(".checkchild").prop("checked", check);
	});
	   ///////列表中的全选
    $("#checkAllticket").click(function(){
    	var check = $(this).prop("checked");
        $(".checkchildTicket").prop("checked", check);
	});
	//上传回执单
	jQuery(".receiptUp").on('click',function(){
		var ticketId=jQuery("#ticketId").val();
		fileUpload(ticketId,"售后回执单",serverTable1,"quoteANum");
	});
	jQuery(".receiptDown").on('click',function(){
		var ticketId=jQuery("#ticketId").val();
    	filedown(ticketId,'售后回执单',1);
	});
});